# IntelligentTeachingBackend/asgi.py
import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack

# 首先设置环境变量（虽然已在命令行设置，但此处添加更安全）
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'IntelligentTeachingBackend.settings')

# 关键步骤：先初始化Django应用，确保应用注册表加载
django_asgi_app = get_asgi_application()

# 之后再导入需要Django模型的模块
import communication.routing  # 现在可以安全导入

application = ProtocolTypeRouter({
    "http": django_asgi_app,
    "websocket": AuthMiddlewareStack(
        URLRouter(
            communication.routing.websocket_urlpatterns
        )
    ),
})